#include <QAction>
#include <QMenu>
#include <QIcon>
#include <QMdiArea>
#include <QActionGroup>
#include <QMdiSubWindow>
#include <QFileDialog>
#include <QMessageBox>
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "spreadsheet.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , pMdiArea(new QMdiArea(this))
    , pActionGroup(new QActionGroup(this))
{
    ui->setupUi(this);
    setCentralWidget(pMdiArea);

    init_actions();
    init_menus();

    setAttribute(Qt::WA_DeleteOnClose);
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::goToContinue()
{
    bool flag(false);

    //get the active spread.
    SpreadSheet* spread = activeSpreadSheet();

    //true spread is existing and modified.
    if ( spread && spread->isWindowModified()) {

        int r = QMessageBox::warning(this, tr("Spreadsheet"),
                                tr("The document has been modified.\n"
                                   "Do you want to save your changes?"),
                                QMessageBox::Yes | QMessageBox::No
                                | QMessageBox::Cancel);

        if (QMessageBox::Yes == r) {
            flag = true;
        } else if (QMessageBox::Cancel == r) {
            flag = false;
        }
    } else if (spread && ( !spread->isWindowModified())) {
        flag = true;
    }

    return flag;
}

void MainWindow::newFile()
{
    addSpreadSheet(SpreadSheet::newFile(this));
}

void MainWindow::open()
{
    SpreadSheet* sheet = SpreadSheet::open(this);

    if ( sheet) {
        addSpreadSheet(sheet);
    }

}

void MainWindow::save()
{
    bool flag(false);
    if (goToContinue()) {
       activeSpreadSheet()->save();
    }
}

bool MainWindow::saveAs()
{
    bool flag(false);
    if ( goToContinue()) {
         flag = activeSpreadSheet()->saveAs();
    } else {
        flag = false;
    }

    return flag;
}

void MainWindow::init_actions()
{
    pActionNew =
            new QAction(QIcon(":/images/new.png"), tr("&New"), this);
    pActionNew->setShortcut(QKeySequence::New);
    pActionNew->setStatusTip(tr("Create a new file"));
    connect(pActionNew, &QAction::triggered,
            this, &MainWindow::newFile);

    pActionOpen =
            new QAction(QIcon(":/images/open.png"), tr("&Open"), this);
    pActionOpen->setShortcut(QKeySequence::Open);
    pActionOpen->setStatusTip(tr("Open a existing file"));
    connect(pActionOpen, &QAction::triggered, this, &MainWindow::open);

    pActionSave =
            new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
    pActionSave->setShortcut(QKeySequence::Save);
    pActionSave->setStatusTip(tr("Save this file"));
    connect(pActionSave, &QAction::triggered, this, &MainWindow::save);

    pActionSaveAs =
            new QAction(tr("Save As"), this);
    pActionSaveAs->setShortcut(QKeySequence::SaveAs);
    pActionSaveAs->setStatusTip(tr("Save as a file"));
    connect(pActionSaveAs, &QAction::triggered,
            this, &MainWindow::saveAs);

    pActionClose = new QAction(tr("Close"), this);
    pActionClose->setStatusTip(tr("Close all spread"));

    pActionExit = new QAction( tr("E&xit"), this);
    pActionExit->setShortcut(QKeySequence::Quit);
    pActionExit->setStatusTip(tr("Quit the application"));
    connect(pActionExit, &QAction::triggered,
            qApp, &QApplication::closeAllWindows);
}

void MainWindow::init_menus()
{
    pMenuFile = ui->menuBar->addMenu(tr("&File"));
    pMenuFile->addAction(pActionNew);
    pMenuFile->addAction(pActionOpen);
    pMenuFile->addAction(pActionSave);
    pMenuFile->addAction(pActionSaveAs);
    pMenuFile->addAction(pActionClose);
    pMenuFile->addSeparator();
    pMenuFile->addAction(pActionExit);

    pMenuWindow = ui->menuBar->addMenu(tr("&Window"));
}

void MainWindow::addSpreadSheet(SpreadSheet *spread)
{
    //add spread's action to menu.
    pMenuWindow->addAction(spread->windowMenuAction());
    pActionGroup->addAction(spread->windowMenuAction());

    //bring the subWindow from spread to show and get the focus.
    QMdiSubWindow* subWindow = pMdiArea->addSubWindow(spread);
    subWindow->show();
    subWindow->setFocus();
}

SpreadSheet* MainWindow::activeSpreadSheet()
{
    QMdiSubWindow* subWindow = pMdiArea->activeSubWindow();
    SpreadSheet* spreadsheet(0);
    if ( subWindow) {
        spreadsheet = qobject_cast<SpreadSheet*>(subWindow->widget());
    } else {
        spreadsheet = 0;
    }

    return spreadsheet;
}
